<?php

namespace App\Http\Controllers\NB\common;

use Illuminate\Http\Request;
use DB;
use App\Http\Controllers\Controller;
use App\Http\Controllers\API\common\NBIotController;

class NbdevController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //查询nb_device
        $data = DB::table('nb_device as nd')
            -> leftJoin('nb_model as nm','nm.id','=','nd.modelId')
            -> select('nd.*','nm.model','nm.manufacturerId','manufacturerName','location','deviceType','protocolType')
            -> paginate(10);
        return view('NB.common.nbdev.index',['data'=>$data,'request'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //获取nb_model数据
        $modelInfo = DB::table('nb_model') -> select('id','name') -> get();
        return view('NB.common.nbdev.create',['modelInfo'=>$modelInfo]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        //获取设备信息
        $info = $request -> except('_token');
        //查询nb设备表,判断设备是否已绑定
        $res = DB::table('nb_device') -> where('verifyCode',$info['verifyCode']) -> first();
        if(!$res){
            //查询nb_model表
            $modelInfo = DB::table('nb_model as nm')
                -> leftJoin('nb_abutment as na','na.id','=','nm.abut_id')
                -> select('nm.*','na.appId','na.secret','na.address')
                -> where('nm.id',$info['modelId'])
                -> first();
            //添加设备
            $NBIot = new NBIotController($modelInfo['appId'],$modelInfo['secret'],$modelInfo['address']);
            $devInfo = $NBIot -> add_devices($info['verifyCode']);
            //dd($devInfo);
            //修改设备
            $output = $NBIot -> update_devices($devInfo['deviceId'],$info['verifyCode'],$modelInfo['manufacturerId'],$modelInfo['manufacturerName'],$modelInfo['deviceType'],$modelInfo['protocolType'],$modelInfo['model']);
            if($output == ''){
                //本地记录设备信息
                $devInfo['addtime'] = time();
                $devInfo['updatetime'] = time();
                $devInfo['name'] = $info['verifyCode'];
                $res = DB::table('nb_device') -> insert(array_merge($devInfo,$info));
                if($res){
                    $data['status'] = 1;
                    $data['msg'] = '设备注册成功';
                }else{
                    $data['status'] = -1;
                    $data['msg'] = 'as设备注册失败';
                }
            }else{
                $data['status'] = 0;
                $data['msg'] = 'ns设备注册失败';
            }
        }else{
            $data['status'] = -2;
            $data['msg'] = '设备已被注册';
        }
        return json_encode($data);
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> input('id');
        //查询nb_device表
        $data = DB::table('nb_device') -> find($id);
        //获取nb_model数据
        $modelInfo = DB::table('nb_model') -> select('id','name') -> get();
        return view('NB.common.nbdev.edit',['data'=>$data,'modelInfo'=>$modelInfo]);

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        //获取设备信息
        $id = $request -> input('id');
        $info = $request -> except('_token','id');
        //查询nb_device表，
        $dev = DB::table('nb_device') -> where('id',$id) -> select('deviceId') -> first();
        $deviceId = $dev['deviceId'];
        //查询nb_model表
        $modelInfo = DB::table('nb_model as nm')
            -> leftJoin('nb_abutment as na','na.id','=','nm.abut_id')
            -> select('nm.*','na.appId','na.secret','na.address')
            -> where('nm.id',$info['modelId'])
            -> first();
        //修改ns设备信息
        $NBIot = new NBIotController($modelInfo['appId'],$modelInfo['secret'],$modelInfo['address']);
        $output = $NBIot -> update_devices($deviceId,$info['verifyCode'],$modelInfo['manufacturerId'],$modelInfo['manufacturerName'],$modelInfo['deviceType'],$modelInfo['protocolType'],$modelInfo['model']);
        if($output == ''){
            //修改as设备信息
            $info['updatetime'] = time();
            $info['name'] = $info['verifyCode'];
            $res = DB::table('nb_device') -> where('id',$id) -> update($info);
            if($res){
                $data['status'] = 1;
                $data['msg'] = '设备修改成功';
            }else{
                $data['status'] = -1;
                $data['msg'] = 'as设备修改失败';
            }
        }else{
             $data['status'] = -1;
             $data['msg'] = 'ns设备修改失败';
        }
        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postDestroy(Request $request)
    {
        $id = $request -> input('id');
        //查询nb_device表
        $dev = DB::table('nb_device') -> where('id',$id) -> select('verifyCode','deviceId','modelId') -> first();
        //查询nb_bind表，是否已绑定设备
        $res = DB::table('nb_bind') -> where('verifyCode',$dev['verifyCode']) -> first();
        if(!$res){
            $deviceId = $dev['deviceId'];
            //查询nb_model表
            $modelInfo = DB::table('nb_model as nm')
                -> leftJoin('nb_abutment as na','na.id','=','nm.abut_id')
                -> select('na.appId','na.secret','na.address')
                -> where('nm.id',$dev['modelId'])
                -> first();
            //删除ns设备
            $NBIot = new NBIotController($modelInfo['appId'],$modelInfo['secret'],$modelInfo['address']);
            $output = $NBIot -> delete_devices($deviceId);
            if($output == ''){
                //删除as设备
                $res = DB::table('nb_device') -> where('id',$id) -> delete();
                if($res){
                    $data['status'] = 1;
                    $data['msg'] = '设备删除成功';
                }else{
                    $data['status'] = -1;
                    $data['msg'] = 'as设备删除失败';
                }
            }else{
                $data['status'] = -1;
                $data['msg'] = 'ns设备删除失败';
            }
        }else{
            $data['status'] = -1;
            $data['msg'] = '设备已被绑定，请先解绑后再删除';
        }
        return json_encode($data);
    }
}
